
import Pages from '../../../pages/pages.vue'
import { showMsg, setMenuType, getPersonalList, updateAdvisor, deleteAdvisor, exportAdvisorList, importAdvisor} from '../../../../vuex/action'

export default {
  name: 'advisor-list',
  components: {
    'vue-pages': Pages
  },
  data() {
    return {
      list: [],
      searchInfo: {
        name: '',
        certificateNo: ''
        // phone: '',
        // orgName: '',
        // status: '',
        // type: '',
        // isNotExist: false
      },
      updateInfo: {
        status: '0'
      },
      teamMemberCount: '',
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.CGBListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xSetMenuType: setMenuType,
      xGetList: getPersonalList,
      xUpdateAdvisor: updateAdvisor,
      xDeleteAdvisor: deleteAdvisor,
      xExport: exportAdvisorList,
      xImportAdvisor: importAdvisor
    }
  },
  ready() {
    this.xSetMenuType('activity')
    // this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      let params= {
        start: start,
        limit: limit
      }
      for( let it in this.searchInfo){
        if(it === 'isNotExist'){
          params[it] = +this.searchInfo[it] + ''
        } else if(this.searchInfo[it]){
          params[it] = this.searchInfo[it]
        }
      }
      this.xGetList(params)
        .then((data) => {
          this.list = data.list
          this.teamMemberCount = data.teamMemberCount
          this.pages.totalPages = Math.ceil(data.totalCount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    updateStatus(it) {
      this.xUpdateAdvisor({
        id: it.id,
        status: it.status
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    updateRegStatus(it) {
      this.xUpdateAdvisor({
        id: it.id,
        regStatus: it.regStatus
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    updateOrgName(it) {
      this.xUpdateAdvisor({
        id: it.id,
        certificateNo: it.certificateNo,
        orgName: it.orgName
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    goMembersPage(id){
      this.$route.router.go({ path: '/team', query: {playerId: id }})
    },
    openPicPage(picUrl){
      window.open(picUrl)
    },
    deleteItem(id){
      this.xDeleteAdvisor({
        id: id
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    exportData(){
      let params = {}
      for( let it in this.searchInfo){
        if(it === 'isNotExist'){
          params[it] = +this.searchInfo[it] + ''
        } else if(this.searchInfo[it]){
          params[it] = this.searchInfo[it]
        }
      }
      this.xExport(params)
    },
    updateRemark(it){
      this.xUpdateAdvisor({
        id: it.id,
        remark: it.remark
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    updateCertificateNo(it){
      this.xUpdateAdvisor({
        id: it.id,
        certificateNo: it.certificateNo
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    importData() {
      var file = this.$els.settlefile.files[0]
      if (file) {
        var formData = new FormData()
        formData.append('file', file)
        this.xImportAdvisor(formData)
          .catch((res) => {
            this.xShowMsg({
              type: 'danger',
              content: res.error_msg
            })
          })
      } else {
        this.xShowMsg({
          type: 'info',
          content: '请选择excel文件'
        })
      }
    },
    search(){
      this.pages.start = 0
      this.getList({})
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
